package cn.hyy.FVOnline.service.impl;

import cn.hyy.FVOnline.entity.Address;
import cn.hyy.FVOnline.entity.City;
import cn.hyy.FVOnline.entity.Province;
import cn.hyy.FVOnline.service.AddressService;
import cn.hyy.FVOnline.mapper.AddressMapper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("addressService")
public class AddressServiceImpl implements AddressService{

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public List<Address> findMyAddress(Integer userId) {
        return addressMapper.findMyAddress(userId);
    }

    @Override
    public JSONObject findToLoadAddress() {
        List<Province> provinces = addressMapper.findAllProvince();
        JSONObject jo = new JSONObject();
        for (Province province : provinces) {
            JSONArray ja = new JSONArray();
            List<City> citys = addressMapper.findPCity(province.getId());
            for (City city : citys) {
                ja.add(city.getCity());
            }
            jo.put(province.getProvince(),ja);
        }
        return jo;
    }

    @Override
    public void delAddress(Integer id) {
        addressMapper.delAddress(id);
    }

    @Override
    public void addAddress(Address address) {
        addressMapper.addAddress(address);
    }
}
